<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------

// 应用公共文件

/**
 * 统一返回信息
 * @param $code
 * @param $data
 * @param $msge
 */
function msg($code, $data, $msg)
{
    return compact('code', 'data', 'msg');
}
/**
 * 整理出tree数据 ---  layui tree
 * @param $pInfo
 * @param $spread
 */
//function getTree($pInfo, $spread = true)
//{
//
//    $res = [];
//    $tree = [];
//    //整理数组
//    foreach($pInfo as $key=>$vo){
//
//        if($spread){
//            $vo['spread'] = true;  //默认展开
//        }
//        $res[$vo['id']] = $vo;
//        $res[$vo['id']]['children'] = [];
//    }
//    unset($pInfo);
//
//    //查找子孙
//    foreach($res as $key=>$vo){
//        if(0 != $vo['pid']){
//            $res[$vo['pid']]['children'][] = &$res[$key];
//        }
//    }
//
//    //过滤杂质
//    foreach( $res as $key=>$vo ){
//        if(0 == $vo['pid']){
//            $tree[] = $vo;
//        }
//    }
//    unset( $res );
//
//    return $tree;
//}

/**
 * 功能：生成二维码
 * @param string $qrData 手机扫描后要跳转的网址
 * @param string $qrLevel 默认纠错比例 分为L、M、Q、H四个等级，H代表最高纠错能力
 * @param string $qrSize 二维码图大小，1－10可选，数字越大图片尺寸越大
 * @param string $savePath 图片存储路径
 * @param string $savePrefix 图片名称前缀
 */
function createQRcode($savePath, $qrData = 'PHP QR Code :)', $qrLevel = 'L', $qrSize = 4, $savePrefix = 'qrcode')
{
    if (!isset($savePath)) return '';
    //设置生成png图片的路径
    $PNG_TEMP_DIR = $savePath;

    //检测并创建生成文件夹
    if (!file_exists($PNG_TEMP_DIR)) {
        mkdir($PNG_TEMP_DIR);
    }
    $filename = $PNG_TEMP_DIR . 'test.png';
    $errorCorrectionLevel = 'L';
    if (isset($qrLevel) && in_array($qrLevel, ['L', 'M', 'Q', 'H'])) {
        $errorCorrectionLevel = $qrLevel;
    }
    $matrixPointSize = 4;
    if (isset($qrSize)) {
        $matrixPointSize = min(max((int)$qrSize, 1), 10);
    }
    if (isset($qrData)) {
        if (trim($qrData) == '') {
            die('data cannot be empty!');
        }
        //生成文件名 文件路径+图片名字前缀+md5(名称)+.png
        $filename = $PNG_TEMP_DIR . $savePrefix . md5($qrData . '|' . $errorCorrectionLevel . '|' . $matrixPointSize) . '.png';
        //开始生成
        import('\phpqrcode\phpqrcode.php');var_dump(new QRcode());die;
        QRcode::png($qrData, $filename, $errorCorrectionLevel, $matrixPointSize, 2);

    } else {
        //默认生成
        \QRcode::png('PHP QR Code :)', $filename, $errorCorrectionLevel, $matrixPointSize, 2);
    }
    if (file_exists($PNG_TEMP_DIR . basename($filename)))
        return basename($filename);
    else
        return FALSE;
}

function phone($phone){
    return  preg_replace('/(1[358]{1}[0-9])[0-9]{4}([0-9]{4})/i','$1****$2',$phone);
}

/**
 * 删除一维数组中的空值
 * @param array $arr
 */

function delNullArray($arr = []){
    $newarr = [];
    foreach ($arr as $key => $val) {
        if (!empty($val)) {
            $newarr[$key] = $val;
        }
    }
    return $newarr;
}

function setAddr($addr){
    $arr = explode('/',$addr);
    unset($arr[3]);
    return implode('/',$arr);
}

/**
 * 支付尾款的师傅佣金提成和推荐人提成
 * @param $money
 * @param $orderid
 * @return bool
 */
function qianbao($money,$orderid){
    $orders = db('orders')->where('order_mun',$orderid)->find();
    $yufukuan = 0;
    foreach ($orders as $vo){
        $yufukuan += $vo['price'];
    }
    $money = $money + $yufukuan;
    $order = $orders[0];
    $userid = $order['sevrice_id'];//查找维修师傅id

    $ticheng = db('ticheng')->where('userid',$userid)->order('id desc')->limit(1)->select();
    $fenlei = db('fenlei')->where('id',$order['fenlei_id'])->find();

    $weixiou_h = db('weixiou_j')->where(['orderid'=>$orderid])->find();

    $yongjin['userid'] = $userid;
    $yongjin['type'] = 1;
    $yongjin['remark'] = '订单'.$orderid.'佣金';
    if ($fenlei['is_chengben'] == 1){//如是按成本提成
        $chengben = $weixiou_h['chenben_price'];
        $money = $money - $chengben;
        $yongjin['money'] = ($money * $fenlei['bili']) /100;//计算师傅佣金
    }else{
        $yongjin['money'] = ($money * $fenlei['bili']) /100;//计算师傅佣金
    }


    $yongjin['addtime'] = time();
    $yongjin['after'] = $ticheng[0]['after'] + ($money * $fenlei['bili']) /100;//求改变后的量
    $yongjin['ticheng'] = 2;
    //添加师傅维修佣金钱包记录
    db('ticheng')->insertGetId($yongjin);
    $user = db('member')->where('id',$userid)->find();//更新总余额
    $re = db('member')->where('id',$userid)->update(['yue'=> $user['yue'] + ($money * $fenlei['bili']) /100]);
    ###################推荐人提成
    unset($user);
    unset($ticheng);
    $pid = db('member')->where('id',$order['memberid'])->find();
    if ($pid > 0){
        $config = db('config')->where('id',1)->find();
        $count = db('orders')->where('memberid',$order['memberid'])->group('order_mun')->count();
        if ($count < $config['tuijian']){
            $ticheng = db('ticheng')->where('userid',$pid['pid'])->order('id desc')->limit(1)->select();
            $tuiguang['userid']  = $pid['pid'];
            $tuiguang['type']    = 1;
            $tuiguang['ticheng']  = 1;
            $tuiguang['addtime'] = time();
            $tuiguang['after'] = $ticheng[0]['after'] + ($money * $config['tuijian_ticheng']) /100;//求改变后的量
            $tuiguang['money']    = ($money*$config['tuijian_ticheng'])/100;
            $tuiguang['remark']  = $pid['user_name'].'订单'.$orderid.'推广提成';
            db('ticheng')->insertGetId($tuiguang);
            $user = db('member')->where('id',$pid['pid'])->find();//更新总余额
            $re = db('member')->where('id',$userid)->update(['yue'=> $user['yue'] + ($money * $config['tuijian_ticheng']) /100]);
        }
    }
    if($re){
        return true;
    }else{
        return false;
    }

}
function new_oreder_mun(){
    $str  = time().rand(1000,9999);
    $order_j = \think\Db::name('weixiou_j')->where('new_oreder_mun',$str)->find();
    if ($order_j){
        new_oreder_mun();
    }
    return $str;
}


